package annotationService.util;

import java.io.*;
import java.util.logging.Logger;

import com.hp.hpl.jena.rdf.model.*;

/**
 * class serves to iterate through RDF file of GEMET Thesaurus
 * @author Andres Westermann
 *
 */
public class RDFIterator extends Object {
	private static Logger log = Logger.getLogger(RDFIterator.class.getSimpleName());
	/**
	 * reading rdf-file and saving all words of vocabulary into one txt-file
	 * @return String[] including all words of the vocabulary
	 * @throws FileNotFoundException
	 */
	public static String[] updateGEMETFile() throws FileNotFoundException {
		
		String[] hits = new String[5209];
		File file = new File(
				"C:/annotationService/resources/gemet-definitions.rdf");
		Model model = ModelFactory.createDefaultModel();
		InputStream in = new FileInputStream(file);
		if (in == null) {
			log.info("\"gemet-definitions.rdf\" not found");
		}
		model.read(in, " ");
		String URI = "http://www.w3.org/2004/02/skos/core#";
		Property prefLabel = model.createProperty(URI + "prefLabel");
		model.setNsPrefix("skos", URI);
		ResIterator iter = model.listSubjectsWithProperty(prefLabel);
		int counter = 0;
		while (iter.hasNext()) { //store each category-name
			Resource res = iter.nextResource();
			String currentPrefLabel = res.getProperty(prefLabel).getString();
			hits[counter] = currentPrefLabel;
			counter++;
		}
		Writer output = null;
		File txtFile = new File(
				"C:/annotationService/resources/GEMET.txt");
		try {
			output = new BufferedWriter(new FileWriter(txtFile));

			for (int i = 0; i < hits.length-1; i++) {
				if(i<hits.length-2){
				output.write(hits[i] + "\r\n");
				}
				else{
					output.write(hits[i]);
				}
			}
			output.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hits;

	}
}